﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Wassrile.Dialogs
{
    public partial class PictPalletDlg : Form
    {
        public PictPalletDlg()
        {
            InitializeComponent();
        }

        private void PictPalletDlg_Load(object sender, EventArgs e)
        {
            if (Core.Config.Behavior.PictPallet.customPalletItems == null || Core.Config.Behavior.PictPallet.customPalletItems.Length == 0)
                customPallet.Enabled = false;
        }

        private void PictPalletDlg_Shown(object sender, EventArgs e)
        {
            if (customPallet.Enabled)
                PalletState = PalletStates.Custom;
            else
                PalletState = PalletStates.Wassr;
            PictPallet.ScrollUpdate();
            if (this.Top < 0)
                this.Top = 0;
        }

        enum PalletStates { Custom, Wassr, All };
        PalletStates _palletState = PalletStates.Custom;
        PalletStates PalletState
        {
            get { return _palletState; }
            set
            {
                _palletState = value;
                customPallet.Checked = value == PalletStates.Custom;
                wassrPallet.Checked = value == PalletStates.Wassr;
                allPallet.Checked = value == PalletStates.All;
                PictPallet.Pictograms = GetPictogramSet();
            }
        }
        private String[] GetPictogramSet()
        {
            switch (PalletState)
            {
                case PalletStates.Custom:
                    return Core.Config.Behavior.PictPallet.customPalletItems;
                case PalletStates.All:
                    return Core.PictogramResolver.GetCodeCandidates().ToArray<string>();
                default:
                    List<string> list = new List<string>();
                    for (int i = 0xE63E; i <= 0xE757; i++)
                        list.Add(i.ToString("X"));
                    return list.ToArray();
            }
        }

        private void customPallet_Click(object sender, EventArgs e)
        {
            PalletState = PalletStates.Custom;
        }

        private void wassrPallet_Click(object sender, EventArgs e)
        {
            PalletState = PalletStates.Wassr;
        }

        private void allPallet_Click(object sender, EventArgs e)
        {
            PalletState = PalletStates.All;
        }

        public event Wassrile.Dialogs.Pictogram.PictogramSet.ClickPallet PalletClicked;

        private void PictPallet_OnClickPallet(object sender, Wassrile.Dialogs.Pictogram.PictogramSet.StringEventArgs e)
        {
            if (PalletClicked != null)
                PalletClicked.Invoke(sender, e);
        }

        private void PictPalletDlg_Deactivate(object sender, EventArgs e)
        {
            if(Core.Config.Behavior.PictPallet.closeOnLostFocus)
            {
                this.Close();
                this.Dispose();
            }
        }

        private void PictPalletDlg_FormClosing(object sender, FormClosingEventArgs e)
        {
            Core.Config.State.PalletSize = this.Size;
            Core.Config.State.PalletLoc = this.Location;
        }
    }
}
